<template>
  <view class="home">
    <view class="home_head">
      <my-search @searchClick="goSearch"></my-search>
    </view>
    <view class="wrap">
      <u-swiper :list="swiperList" name="image_src" mode="dot" height="330" @click="btnSwiper"></u-swiper>
    </view>
    <view class="nav">
      <view class="nav_data" v-for="(item,index) in navList" :key="index" @click="navClickHand(item)">
        <image :src="item.image_src" mode=""></image>
      </view>
    </view>
    <view class="floor">
      <view class="floor_data" v-for="(item,index) in floorList" :key="index">
        <image :src="item.floor_title.image_src" mode="" class="img_src"></image>
        <view class="floor_flex">
          <view class="floor_product">
            <!-- 左侧大盒子 -->
            <view class="left_big">
              <image :src="item.product_list[0].image_src" mode="widthFix"
                :style="{width:item.product_list[0].image_width + 'rpx'}" @click="imgBig(item.product_list[0])"></image>
            </view>
            <!-- 左侧四个小盒子 -->
            <view class=" right_min">
              <view v-for="(itemProduct,i) in item.product_list" :key="i" v-if="i !== 0" @click="imgSmall(itemProduct)">
                <image :src="itemProduct.image_src" mode="widthFix" :style="{width:itemProduct.image_width+'rpx'}">
                </image>
              </view>
            </view>
          </view>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
  import {
    swiperData,
    catItems,
    floorData
  } from '@/http/api.js'
  export default {
    data() {
      return {
        // 1. 轮播图的数据列表，默认为空数组
        swiperList: [],
        // 保存分类导航数据
        navList: [],
        // 楼层数据
        floorList: []
      };
    },
    onLoad() {
      this.getSwiperList()
      this.getNavList()
      this.getFloorList()
    },
    methods: {
      // 获取轮播图数据
      async getSwiperList() {
        const {
          data: res
        } = await swiperData()
        if (res.meta.status !== 200) return uni.$showMsg()
        this.swiperList = res.message
      },
      // 点击轮播图
      btnSwiper(index) {
        let swiperTO = this.swiperList[index].goods_id
        uni.navigateTo({
          url: '../../subpkg/goods_detail/goods_detail?goods_id=' + swiperTO
        })
      },
      // 获取分类导航数据
      async getNavList() {
        const {
          data: res
        } = await catItems()
        if (res.meta.status !== 200) return uni.$showMsg()
        this.navList = res.message
      },
      // 跳转到分类
      navClickHand(item) {
        if (item.name === '分类') {
          uni.switchTab({
            url: '/pages/cate/cate'
          })
        }
      },
      // 获取楼层数据
      async getFloorList() {
        const {
          data: res
        } = await floorData()
        if (res.meta.status !== 200) return uni.$showMsg()
        this.floorList = res.message
        this.floorList.forEach(floor => {
          floor.product_list.forEach(proUrl => {
            proUrl.url = '/subpkg/goods_list/goods_list?' + proUrl.navigator_url.split('?')[1]
          })
        })
      },
      // 点击左侧大图片
      imgBig(item) {
        uni.navigateTo({
          url: item.url
        })
      },
      // 点击右侧小图片
      imgSmall(item) {
        uni.navigateTo({
          url: item.url
        })
      },
      // 跳转到搜索页
      goSearch() {
        uni.navigateTo({
          url: '/subpkg/search/search'
        })
      }
    }
  }
</script>

<style lang="scss">
  .home_head {
    position: sticky;
    top: 0;
    z-index: 999;
  }

  .nav {
    display: flex;
    justify-content: space-around;
    margin: 15rpx 0;

    .nav_data {
      image {
        width: 128rpx;
        height: 140rpx;
      }
    }
  }

  .floor {
    .floor_data {
      .img_src {
        display: flex;
        width: 100%;
        height: 60rpx;
      }

      .floor_flex {

        .floor_product {
          display: flex;
          padding-left: 10rpx;

          .right_min {
            display: flex;
            flex-wrap: wrap;
            justify-content: space-around;
          }
        }
      }

    }
  }
</style>
